#!/usr/bin/env python
# coding=utf-8
from flask import Blueprint

from plant import logger
from plant.base.beanret import BeanRet
from plant.base.tools.config import Config
from plant.service.DHTSV import DHT

pitop = Blueprint('temandhumi', __name__)


@pitop.route('/temandhumi/read')
def read():
    '''
    read tem and humi
    :return:
    '''
    dht_type = int(Config().read("temperature_humidity", "dht_type"))
    pin = int(Config().read("temperature_humidity", "pin"))
    sensor = DHT(str(dht_type), int(pin))
    humi, temp = sensor.read()
    if not humi is None:
        logger.info('DHT{0}, humidity {1:.1f}%, temperature {2:.1f}*'.format(sensor.dht_type, humi, temp))
    else:
        logger.info('DHT{0}, humidity & temperature: {1}'.format(sensor.dht_type, temp))

    return BeanRet(success=True, data={'humi': humi, 'temp': temp}).toJson()
